# Author: Chris Moyer
import urllib
import marajo

if marajo.config:
	auth_type = marajo.config.get("auth_type", "basic")
	if auth_type.lower() == "openid":
		from marajo.appengine.api.users.openid import User
	else:
		from marajo.appengine.api.users.basic import User


class Error(Exception):
	"""Base User error type."""

class UserNotFoundError(Error):
	"""
	Raised if the user id isn't found and no user is
	logged in.
	"""

def create_login_url(url):
	"""
	Create the login url, returning to "url" after we finished
	"""
	return "/_ah/login?continue=%s" % urllib.quote_plus(url)

def create_logout_url(url):
	"""
	Create a logout url, returning to "url" after we finished
	"""
	return "/_ah/logout?continue=%s" % urllib.quote_plus(url)

def get_current_user(session):
	try:
		user = User.get_by_ids(session.get("user"))
	except:
		user = None
	return user
